﻿using AutoMapper;
using Comm100.Application.Services;
using Comm100.Framework.Auditing;
using Comm100.Framework.Authorization;
using Comm100.Public.Audit;
using Bot.Application.Dto;
using Bot.Application.Services;
using Bot.Domain.Bo;
using Bot.Domain.Entities;
using Bot.Domain.Interfaces;
using System;
using System.Collections.Generic;
using System.Text;
using Bot.Domain;
using Bot.Domain.Enum;

namespace Bot.Application.QuickReplies.Service
{
    public class QuickReplyAppService : BaseAppService, IQuickReplyAppService
    {
        private IQuickReplyDomainService _domainService;
        private IQuickReplyVerifyDataManager _quickReplyVerifyManager;
        private IBotVerifyDataManager _botVerifyManager;

        public QuickReplyAppService(IQuickReplyDomainService domainService,
            IQuickReplyVerifyDataManager quickReplyVerifyManager,
            IBotVerifyDataManager botVerifyManager) : base()
        {
            this._domainService = domainService;
            this._quickReplyVerifyManager = quickReplyVerifyManager;
            this._botVerifyManager = botVerifyManager;
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.QUICKREPLY, AuditAction.DESTROY)]
        public void Delete(Guid id)
        {
            var errorInfo = _quickReplyVerifyManager.VerifyId(id);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            _domainService.Delete(id);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.QUICKREPLY, AuditAction.CREATE)]
        public Dto.QuickReplyDto Add(Guid botId, QuickReplyCreateDto dto)
        {
            dto.Id = Guid.NewGuid();
            if (string.IsNullOrEmpty(dto.Name))
            {
                dto.Name = dto.Id.ToString();
            }
            var errorInfo = _botVerifyManager.VerifyId(botId);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            errorInfo = _quickReplyVerifyManager.VerifyCreate(botId, dto);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            dto.BotId = botId;
            QuickReply quickReply = _domainService.Create(Mapper.Map<QuickReply>(dto));
            return Mapper.Map<Dto.QuickReplyDto>(quickReply);
        }

        [Authorization(BotPermission.MANAGE_BOTS)]
        //[Audit(BotEntity.QUICKREPLY, AuditAction.UPDATE)]
        public Dto.QuickReplyDto Update(QuickReplyUpdateDto dto)
        {
            if (string.IsNullOrEmpty(dto.Name))
            {
                dto.Name = dto.Id.ToString();
            }
            var errorInfo = _quickReplyVerifyManager.VerifyUpdate(dto);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }

            QuickReply quickReply = _domainService.Update(Mapper.Map<QuickReplyUpdateBo>(dto));
            return Mapper.Map<Dto.QuickReplyDto>(quickReply);
        }

        public Dto.QuickReplyDto Get(Guid id, string include)
        {
            var errorInfo = _quickReplyVerifyManager.VerifyId(id);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            QuickReply quickReply = _domainService.GetById(id, include);
            return Mapper.Map<Dto.QuickReplyDto>(quickReply);
        }

        public IEnumerable<Dto.QuickReplyDto> GetList(Guid botId, string type, string include)
        {
            var errorInfo = _botVerifyManager.VerifyId(botId);
            if (errorInfo.Code != 1)
            {
                throw new BusinessException(errorInfo);
            }
            if (!string.IsNullOrEmpty(type))
            {
                errorInfo = _quickReplyVerifyManager.VerifyType(type);
                if (errorInfo.Code != 1)
                {
                    throw new BusinessException(errorInfo);
                }
            }
            IEnumerable<QuickReply> list;
            if (string.IsNullOrEmpty(type))
            {
                list = _domainService.List(botId, include);
            } else
            {
                list = _domainService.List(botId, (EnumReplyType)Enum.Parse(typeof(EnumReplyType), type), include);
            }
            return Mapper.Map<IEnumerable<Dto.QuickReplyDto>>(list);
        }
    }
}
